const md = require('../../utils/md5.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName: 'JACK李',
    avatarUrl: '/images/ZKQB.png',
    model: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.fromid) {
      that.setData({
        fromid: options.fromid
      })
      wx.setStorageSync('fromid', options.fromid);
    }
    if (wx.getStorageSync('avatarUrl')){
      that.setData({
        avatarUrl: wx.getStorageSync('avatarUrl')
      })
    }
    var sign = wx.getStorageSync('nickName');
    console.log(sign)
    if(sign){
      that.setData({
        model: 1
      })
    }
    // wx.getUserInfo({
    //   success: function (res) {
    //     that.setData({
    //       nickName: res.userInfo.nickName,
    //       avatarUrl: res.userInfo.avatarUrl,
    //     })
    //   },
    // });
    // that.getopenid();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    if (e.detail.value.phone == '') {
      wx.showModal({
        title: '',
        content: '手机号不能为空',
      })
      return;
    }
    if (e.detail.value.password == '') {
      wx.showModal({
        title: '',
        content: '密码不能为空',
      })
      return;
    }
    wx.showLoading({
      title: '正在登陆',
    })
    // 获取时间戳
    var apinameIsMeb = "/Action/PK01/MemberLogin";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    console.log(md.hexMD5(e.detail.value.password));
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        mobile: e.detail.value.phone,
        password: md.hexMD5(e.detail.value.password)
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        wx.hideLoading();
        if (res.data.data == true) {
          wx.setStorageSync('phone', e.detail.value.phone);
          wx.switchTab({
            url: '/pages/index/index',
          })
        } else {
          wx.showModal({
            title: '',
            content: '账号或密码错误！',
          })
          return;
        }
      },
      fail: function (res) {
        console.log(res)
        wx.hideLoading();
        wx.showModal({
          title: '',
          content: "error：" + res.data.msg,
        })
        return;
      }
    })

  },
  getuserinfo: function (e) {
    console.log(e);
    this.setData({
      nickName: e.detail.userInfo.nickName,
      avatarUrl: e.detail.userInfo.avatarUrl,
      model:1
    })
    wx.setStorageSync('avatarUrl', e.detail.userInfo.avatarUrl);
    wx.setStorageSync('nickName', e.detail.userInfo.nickName);
    var openId = (wx.getStorageSync('openId'));
    if (openId) {
      return;
    } else {
      wx.login({
        success: function (res) {
          console.log(res.code);
          // 获取时间戳
          var apinameIsMeb = "/Action/PK01/GetMebInfo";
          var timestamp = new Date().getTime();
          timestamp = parseInt(timestamp / 1000);
          var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
          if (res.code) {
            wx.request({
              //后台接口地址
              url: app.server.url + apinameIsMeb,
              data: {
                code: res.code,
                sign: md5IsMeb,
                appid: app.server.appid,
                tmsp: timestamp,
              },
              method: 'POST',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              success: function (res) {
                // this.globalData.userInfo = JSON.parse(res.data);
                console.log(res);
                if (res.data.code != 0) {
                  console.log("获取失败！")
                } else {
                  wx.setStorageSync('openId', res.data.data.openid);
                }
              }
            })
          }
        }
      })
    }
  }
})